import { requestClient } from '#/api/request';
import { RequestClient } from '@vben/request';

// 创建基础请求客户端，用于需要原始响应数据的请求
const baseRequestClient = new RequestClient({ baseURL: 'http://localhost:8000' });

export namespace AuthApi {
  /** 登录接口参数 */
  export interface LoginParams {
    password?: string;
    username?: string;
  }

  /** 登录接口返回值 */
  export interface LoginResult {
    accessToken: string;
  }

  export interface RefreshTokenResult {
    data: string;
    status: number;
  }
}

/**
 * 登录
 */
export const loginApi = (data: AuthApi.LoginParams): Promise<AuthApi.LoginResult> => {
  return requestClient.post('/system/login/', data);
};

/**
 * 刷新accessToken
 */
export const refreshTokenApi = (): Promise<AuthApi.RefreshTokenResult> => {
  return baseRequestClient.post('/auth/refresh', {
    withCredentials: true,
  });
};

/**
 * 退出登录
 */
export const logoutApi = (): Promise<any> => {
  return baseRequestClient.post('/system/logout/', {
    withCredentials: true,
  });
};

/**
 * 获取用户权限码
 */
export const getAccessCodesApi = (): Promise<string[]> => {
  return requestClient.get('/system/codes/');
};
